<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
//use Illuminate\Support\Facades\Schema;

class AddTable20231019 extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('course_class')) {
            Schema::create('course_class', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('pid')->index()->default(0)->comment('上级ID');
                $table->string('name')->comment('名称');
                $table->string('img')->nullable()->comment('图片');
                $table->integer('sort')->default(0)->comment('排序值');
                $table->tinyInteger('status')->default(1)->comment('状态 0-禁用 1-正常');
                $table->timestamps();
                $table->comment = '课程分类表';
            });
        }

        if (!Schema::hasTable('course')) {
            Schema::create('course', function (Blueprint $table) {
                $table->id();
                $table->string('name')->comment('名称');
                $table->integer('class_id_1')->index()->comment('顶级分类ID');
                $table->integer('class_id_2')->comment('二级分类ID');
                $table->string('cover')->nullable()->comment('封面图');
                $table->text('banners')->nullable()->comment('banner图');
                $table->text('details')->nullable()->comment('详情');
                $table->integer('watch_num')->default(0)->comment('学习人数');
                $table->tinyInteger('is_pay')->default(0)->comment('是否免费');
                $table->integer('pay_amount')->default(0)->comment('购买金额 单位分');
                $table->tinyInteger('vip_is_pay')->default(0)->comment('vip用户是否免费');
                $table->integer('vip_pay_amount')->default(0)->comment('vip购买金额 单位分');
                $table->tinyInteger('status')->default(0)->comment('状态 0-禁用 1-显示');
                $table->tinyInteger('is_recommend')->default(0)->comment('是否推荐');
                $table->tinyInteger('sort')->default(0)->comment('排序值');
                $table->timestamps();
                $table->comment = '课程表';
            });
        }

        if (!Schema::hasTable('course_video')) {
            Schema::create('course_video', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('course_id')->index()->comment('课程ID');
                $table->unsignedBigInteger('course_source_id')->index()->comment('课程素材ID');
                $table->integer('sort')->default(0)->comment('排序值');
                $table->timestamps();
                $table->comment = '课程视频表';
            });
        }

        if (!Schema::hasTable('course_source_class')) {
            Schema::create('course_source_class', function (Blueprint $table) {
                $table->id();
                $table->string('name')->comment('名称');
                $table->integer('sort')->default(0)->comment('排序值');
                $table->timestamps();
                $table->comment = '课程资源分类表';
            });
        }

        if (!Schema::hasTable('course_source')) {
            Schema::create('course_source', function (Blueprint $table) {
                $table->id();
                $table->string('name')->comment('名称');
                $table->unsignedBigInteger('course_source_class_id')->index()->comment('素材分类ID');
                $table->string('type')->comment('类型 video-视频');
                $table->string('cover')->nullable()->comment('封面图');
                $table->string('file')->nullable()->comment('文件路径');
                $table->integer('sort')->default(0)->comment('排序值');
                $table->tinyInteger('status')->default(1)->comment('状态 0-禁止 1-正常');
                $table->timestamps();
                $table->comment = '课程素材表';
            });
        }

        if (!Schema::hasTable('course_comment')) {
            Schema::create('course_comment', function (Blueprint $table) {
                $table->id();
                $table->unsignedInteger('member_id')->index()->comment('用户ID');
                $table->unsignedBigInteger('course_id')->index()->comment('课程ID');
                $table->string('content')->comment('评论内容');
                $table->tinyInteger('status')->default(0)->comment('审核状态 0-未审核 1-审核通过 2-审核不通过');
                $table->timestamps();
                $table->comment = '课程评论表';
            });
        }

        if (!Schema::hasTable('course_video_history')) {
            Schema::create('course_video_history', function (Blueprint $table) {
                $table->id();
                $table->unsignedInteger('member_id')->comment('用户ID');
                $table->unsignedBigInteger('course_id')->comment('课程ID');
                $table->unsignedBigInteger('course_video_id')->comment('课程视频ID');
                $table->integer('pace')->default(0)->comment('观看进度(百分比)');
                $table->timestamps();
                $table->index(['member_id', 'course_id']);
                $table->index(['member_id', 'course_id', 'course_video_id']);
                $table->comment = '课程视频观看历史表';
            });
        }

        if (!Schema::hasTable('course_order')) {
            Schema::create('course_order', function (Blueprint $table) {
                $table->id();
                $table->unsignedInteger('member_id')->index()->comment('用户ID');
                $table->string('mini_type')->comment('小程序类型');
                $table->string('order_sn')->index()->comment('订单号');
                $table->string('pay_sn')->index()->comment('支付号');
                $table->integer('pay_amount')->comment('支付金额 单位分');
                $table->integer('pay_time')->default(0)->comment('支付时间');
                $table->tinyInteger('status')->default(0)->comment('支付状态 0-未支付 1-已支付 2-已退款');
                $table->unsignedBigInteger('course_id')->comment('课程ID');
                $table->string('course_name')->comment('课程名称');
                $table->string('course_cover')->comment('课程封面图');
                $table->integer('course_price')->comment('课程价格');
                $table->integer('course_vip_price')->comment('课程vip价格');
                $table->timestamps();
                $table->index(['member_id', 'course_id']);
                $table->comment = '课程订单表';
            });
        }

        Schema::table('members', function (Blueprint $table) {
            if (!Schema::hasColumn('members', 'vip_show_adv')) {
                $table->tinyInteger('vip_show_adv')->default(0)->comment('vip是否显示广告');
            }
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
